/*
 * codename: xEngine
 * Copyright (C) Aljosa Osep <aljosa.osep@gmail.com>
 *
 * Based on xSoko engine (C) Aljosa Osep, Jernej Skrabec, Jernej Halozan, Martin Savc 2008 <aljosa.osep@gmail.com, jernej.skrabec@gmail.com, jernej.halozan@gmail.com, martin.savc@gmail.com>
 *
 * xEngine project is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * xEngine project is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Avtor: Jernej Skrabec

#include <map>
#include <fstream>
#include "config.h"
#include "CommonStructures.h"
#include "messages.h"

namespace xEngine
{
    namespace CoreSystem
    {
        namespace Config
        {
            typedef map<string,string>::iterator Iterator;
            map<string,string> _content;

            string Trim(string str)
            {
                size_t startpos = str.find_first_not_of(" ");
                size_t endpos = str.find_last_not_of(" ");
                return str.substr( startpos, endpos-startpos+1 );
            }

            bool ReadConfig(string configFile)
            {
                ifstream file(configFile.c_str());
                if(!file.good()) return false;

                string line;
                string name;
                int posEqual;

                for (int count=0;getline(file,line);count++) {
                    if (!line.length()) continue;
                    if (line[0] == '#') continue; //commentar symbol
                    if (line[0] == ';') continue; //commentar symbol

                    posEqual = line.find('=');
                    if(posEqual != string::npos)
                    {
                        name  = Trim(line.substr(0,posEqual));
                        _content[name] = Trim(line.substr(posEqual+1));
                    }
                    else
                    {
                        Messages::errorMessage("Invalid line " + Functions::toString(count) + " in config file!");
                    }
                }

                file.close();
                return true;
            }

            int GetValueInt(string name)
            {
                Iterator it = _content.find(name);
                if(it != _content.end())
                    return Functions::strToInt(it->second);
                else
                    return 0;
            }

            string GetValueString(string name)
            {
                Iterator it = _content.find(name);
                if(it != _content.end())
                    return it->second;
                else
                    return NULL;
            }

            bool GetValueBool(string name)
            {
                return GetValueInt(name);
            }

            void SetValueInt(string name, int value)
            {
                _content[name] = Functions::toString(value);
            }

            void SetValueString(string name, string value)
            {
                _content[name] = value;
            }

            void SetValueBool(string name, bool value)
            {
                _content[name] = Functions::toString(value);
            }

            bool SaveConfig(string configFile)
            {
                ofstream file(configFile.c_str());
                if(file.bad()) return false;

                for (Iterator it=_content.begin() ; it != _content.end(); it++)
                {
                    file << it->first << "=" << it->second << endl;
                }

                file.close();
                return true;
            }
        }
    }

}
